package com.lvmama.rhino.service.impl.comment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.mapperComment.CategoryRankMapper;
import com.lvmama.rhino.pojo.comment.entity.vo.*;
import com.lvmama.rhino.pojo.comment.request.CategoryCommentRateRequest;
import com.lvmama.rhino.pojo.comment.request.CategoryProductRankRequest;
import com.lvmama.rhino.pojo.comment.request.CategoryRankChartRequest;
import com.lvmama.rhino.pojo.comment.request.CategoryRankRequest;
import com.lvmama.rhino.service.comment.CategoryRankService;
import com.lvmama.rhino.utils.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 品类排名
 */

@Service
@Transactional
public class CategoryRankServiceImpl implements CategoryRankService {

    @Autowired
    private CategoryRankMapper categoryRankMapper;

    @Override
    public List<CategoryRankVO> getCategoryRank(CategoryRankRequest request) {
        List<CategoryRankVO> list = categoryRankMapper.getCategoryRank(request);
        return list;
    }

    @Override
    public List<CategoryRankChartVO> getCategoryRankChart(CategoryRankChartRequest request) {
        List<CategoryRankChartVO> list = categoryRankMapper.getCategoryRankChart(request);
        return list;
    }

    @Override
    public PageInfo<CategoryProductRankVO> getCategoryProductRank(CategoryProductRankRequest request) {
        PageHelper.startPage(request.getCurrentPage(), request.getPageSize());
        PageInfo<CategoryProductRankVO> list = new PageInfo<>(categoryRankMapper.getCategoryProductRank(request));

        // 产品名称
        if (null != list && !CollectionUtils.isEmpty(list.getList())) {
            for (int i = 0; i < list.getList().size(); i++) {
                CategoryProductRankVO vo = list.getList().get(i);
                String productName = vo.getProductName();
                if (StringUtils.isBlank(productName)) {
                    productName = Constants.UNKNOWN;
                } else if (Constants.UNKNOWNDATA.equals(productName.trim()) ||
                        Constants.UNKNOWNDATA2.equals(productName.trim()) ||
                        Constants.UNKNOWNDATA3.equalsIgnoreCase(productName.trim())) {
                    productName = Constants.UNKNOWN;
                }
                vo.setProductName(productName);
            }
        }


        return list;
    }

    /**
     * 品类满意度分析
     *
     * @param request
     * @return
     */
    @Override
    public List<CategoryCommentCountVO> getCategoryCommentCount(CategoryCommentRateRequest request) {
        List<CategoryCommentCountVO> list = categoryRankMapper.getCategoryCommentCount(request);
        return list;
    }

    /**
     * 满意度占比
     *
     * @param request
     * @return
     */
    @Override
    public CategoryCommentRateVO getOneCommentRate(CategoryCommentRateRequest request) {
        CategoryCommentRateVO vo = categoryRankMapper.getOneCommentRate(request);
        return vo;
    }

    /**
     * 满意度趋势
     *
     * @param request
     * @return
     */
    @Override
    public List<CategoryCommentOrientVO> getOneCommentOrient(CategoryCommentRateRequest request) {
        List<CategoryCommentOrientVO> orientVOList = categoryRankMapper.getOneCommentOrient(request);
        return orientVOList;
    }

}
